<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 新增按钮 -->
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="showAdd"
            >添加角色</el-button>
            <el-table
              v-loading="loading"
              :data="list"
              border
              style="margin-top: 10px"
            >
              <el-table-column
                label="序号"
                type="index"
                width="120"
                :index="indexMethod"
              />
              <el-table-column label="角色名称" prop="name" width="120" />
              <el-table-column label="描述" prop="description" />
              <el-table-column align="center" label="操作" width="360">
                <template v-slot="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="showEdit(row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end">
              <!--
                total:总共有多少条数据
                page-size:每页显示几条
                current-page:当前显示第几页
                background:带背景的分页
                layout:控制要分页显示哪些内容
               -->
              <el-pagination
                :total="total"
                :page-size="pagesize"
                :current-page="page"
                background
                layout="total,prev, pager, next"
                @current-change="handleChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
              style="margin-bottom: 20px"
            />
            <el-row>
              <el-col :span="12">
                <el-form label-width="100px">
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="form.id ? '编辑角色' : '新增角色'"
        :visible="showDialog"
        width="40%"
        @close="closeDialog"
      >
        <el-form ref="form" label-width="80px" :model="form" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" @click="closeDialog">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="addRole"
            >确定</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  addRole,
  getRoleDetail,
  updateRole,
  deleteRole,
  getCompanyById
} from '@/api/settings'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      pagesize: 2,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '角色名称不能为空',
            trigger: ['blur', 'change']
          }
        ]
      },
      company: {}
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyById()
  },
  methods: {
    async getRoleList() {
      this.loading = 'true'
      const res = await getRoleList(this.page, this.pagesize)
      this.list = res.rows
      this.total = res.total
      this.loading = false
    },
    handleChange(value) {
      console.log('我执行了吗', value)
      this.page = value
      this.getRoleList()
    },
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    showAdd() {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    async addRole() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        // 更新
        await updateRole(this.form)
      } else {
        await addRole(this.form)
        // 跳转到最后一页
        this.total++
        this.page = Math.ceil(this.total / this.pagesize)
      }

      this.$message.success('操作成功')
      // 关闭弹出框
      this.showDialog = false
      // 重新渲染
      this.getRoleList()
    },
    async showEdit({ id }) {
      // console.log('编辑')
      // this.form = row
      const res = await getRoleDetail(id)
      console.log(res)
      this.form = res
      this.showDialog = true
    },
    async delRole(id) {
      try {
        await this.$confirm('你确定要删除吗', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      // 发送请求删除
      await deleteRole(id) // 调用删除接口
      this.$message.success('删除角色成功')
      if (this.list.length === 1 && this.page > 1) {
        this.page--
      }
      this.getRoleList() // 重新加载数据
    },
    // async getCompanyById() {
    //   const res = await getCompanyById(this.userInfo.companyId)
    //   this.company = res
    // }
    async getCompanyById() {
      const data = await getCompanyById(this.userInfo.companyId)
      console.log(data)
      this.company = data
    }
  }
}
</script>

<style>
</style>
